#ifndef CONSOLE_H
#define CONSOLE_H

#include "comm/types.h"
#include "dev/tty.h"
#include "ipc/mutex.h"

#define CONSOLE_DISP_ADDR 0xb8000
#define CONSOLE_DISP_SIZE (32*1024)
#define CONSOLE_DISP_END (CONSOLE_DISP_ADDR + CONSOLE_DISP_SIZE)
#define CONSOLE_ROW_MAX 25
#define CONSOLE_COL_MAX 80

// esc字符的的编码，用以判断是否为esc转义字符串
#define ANSI_ESC 0x1b
// esc转义字符串参数
#define ESC_PARAM_MAX 10

typedef enum
{
    COLOR_Black = 0,
    COLOR_Blue,
    COLOR_Green,
    COLOR_Cyan,
    COLOR_Red,
    COLOR_Magenta,
    COLOR_Brown,
    COLOR_Gray,
    COLOR_DackGray,
    COLOR_LightBlue,
    COLOR_LightGreen,
    OLOR_LightCyan,
    COLOR_LightRed,
    COLOR_LightMagenta,
    COLOR_Yellow,
    COLOR_White
}color_t;

// 每一个字符的属性
typedef union _disp_char_t
{
    struct
    {
        char c;
        char foreground :4;
        char background :3;
    };
    
    uint16_t v;
}disp_char_t;

// 控制台
typedef struct _console_t
{
    enum{
        CONSOLE_WRITE_NORMAL,
        // 遇到 \0337
        CONSOLE_WRITE_ESC,
        // 遇到 \033[
        CONSOLE_WRITE_SQUARE
    }write_status;
    
    disp_char_t * disp_base;
    int display_row, display_col;
    // 光标所在行、所在列
    int cursor_row, cursor_col;
    // 屏幕前景色、背景色
    char foreground, background;
    // 保存光标的位置
    int old_cursor_row, old_cursor_col; 
    // esc转义字符串的参数
    int esc_param[ESC_PARAM_MAX];
    int esc_param_index;

    mutex_t mutex;
}console_t;

int console_init(int console_idx);
int console_write(tty_t * tty);
void console_close(int console);
void console_select(int tty_idx);
#endif // !CONSOLE_H